package com.example.lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/21 下午10:19
 */
public class LockUtils {

    static Logger logger = LoggerFactory.getLogger(LockUtils.class);

    public static void lockInAction(DataSource dataSource, LockCode lockCode) {
        Lock lock = null;
        try {
            lock = new Lock(dataSource);
            lock.lock();
            lockCode.sync();
        } catch (Exception e) {
            logger.warn("lock error : {}", e.getMessage());
        } finally {
            if (lock != null) {
                lock.unlock();
            }
        }
    }

    @FunctionalInterface
    public interface LockCode {

        void sync();
    }
}
